from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.charts import Line
from pyecharts.components import Table
from pyecharts.charts import WordCloud
from pyecharts.charts import Pie
from pyecharts.charts import Funnel
from pyecharts.charts import PictorialBar
from pyecharts.options import ComponentTitleOpts
from pyecharts.globals import SymbolType
import json
#1.画出每日累计确诊病例数和死亡数--双柱图
def drawChart_1(index):
    root="/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result"+str(index)+"/part-00000.json"
    date=[]
    cases=[]
    deaths=[]
    with open(root,'r') as f:
        while True:
            line=f.readline()
            if not line:                #到EOF，返回空字符串，终止循环
                break
            js=json.loads(line)
            date.append(str(js['date']))
            cases.append(int(js['cases']))
            deaths.append(int(js['deaths']))
    #Bar():柱形图  横坐标：日期 两个纵坐标：双柱
    d=(
        Bar().add_xaxis(date).add_yaxis("累计确诊人数",cases,stack="stack1").add_yaxis("累计死亡人数",deaths,stack="stack1")
             .set_series_opts(label_opts=opts.LabelOpts(is_show=False)).set_global_opts(title_opts=opts.TitleOpts(title="美国每日累计确诊和死亡人数"))
             .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result1/result1.html")
    )


#2.画出每日较昨日新增确诊病例数和死亡数--折线图
def drawChart_2(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    date = []
    cases = []
    deaths = []
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            date.append(str(js['date']))
            cases.append(int(js['caseIncrease']))
            deaths.append(int(js['deathIncrease']))
    #确诊折线图
    #Line:折线图，横坐标是日期，纵坐标是新增数。在折线图中标记一个点和一条线：点是最大值，线是平均值
    d1=(
        Line(init_opts=opts.InitOpts(width="1600px", height="800px")).add_xaxis(xaxis_data=date)
            .add_yaxis(series_name="新增确诊",y_axis=cases,
                       markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值")]),
                       markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值")]),
        )
            .set_global_opts(title_opts=opts.TitleOpts(title="美国每日新增确诊折线图", subtitle=""),
                             tooltip_opts=opts.TooltipOpts(trigger="axis"),
                             toolbox_opts=opts.ToolboxOpts(is_show=True),
                             xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
                             )
            .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result2/result1.html")

    )
    #死亡折线图
    #在折线图中标记一个点和两条线：点是最大值，线是平均值和最高点
    d2=(
        Line(init_opts=opts.InitOpts(width="1600px", height="800px")).add_xaxis(xaxis_data=date)
            .add_yaxis(series_name="新增死亡", y_axis=deaths,
                       markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="最大值")]),
                       markline_opts=opts.MarkLineOpts(data=[opts.MarkLineItem(type_="average", name="平均值"),
                                                              opts.MarkLineItem(symbol="none", x="90%",y="max"),
                                                              opts.MarkLineItem(symbol="circle", type_="max", name="最高点"),
                                                              ]),
                       )
            .set_global_opts(title_opts=opts.TitleOpts(title="美国每日新增死亡折线图", subtitle=""),
                             tooltip_opts=opts.TooltipOpts(trigger="axis"),
                             toolbox_opts=opts.ToolboxOpts(is_show=True),
                             xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
                             )
            .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result2/result2.html")
    )
#3.画出统计截止5.19日 美国各州累计确诊人数和死亡人数---表格
def drawChart_3(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    allState = []
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            row=[]
            row.append(str(js['state']))
            row.append(int(js['sumCases']))
            row.append(int(js['sumDeaths']))
            row.append(float(js['deathRate']))
            allState.append(row)
    table=Table()
    headers=["State name","Total cases","Total deaths","Death rate"]
    rows=allState
    table.add(headers,rows)
    table.set_global_opts(title_opts=ComponentTitleOpts(title="美国各州疫情一览",subtitle=""))
    table.render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result3/result1.html")

#4.画出美国确诊最多的10个州--词云图
def drawChart_4(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    data = []
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            row=(str(js['state']),int(js['sumCases']))
            data.append(row)
    d=(
        WordCloud().add("",data,word_size_range=[20,100],shape=SymbolType.DIAMOND).set_global_opts(title_opts=opts.TitleOpts(title="美国各州确诊Top10"))
        .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result4/result1.html")
    )

#5.画出美国死亡最多的10个州--象柱状图
def drawChart_5(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    state=[]
    sumDeath=[]
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            state.insert(0,str(js['state']))
            sumDeath.insert(0,int(js['sumDeaths']))
    d=(
        PictorialBar().add_xaxis(state).add_yaxis("",sumDeath,label_opts=opts.LabelOpts(is_show=False),symbol_size=18,symbol_repeat="fixed",
                                                  symbol_offset=[0,0],is_symbol_clip=True,symbol=SymbolType.ROUND_RECT)
        .reversal_axis().set_global_opts(title_opts=opts.TitleOpts(title="PictorialBar-美国各州死亡人数Top10"),
                                         xaxis_opts=opts.AxisOpts(is_show=False),
                                         yaxis_opts=opts.AxisOpts(axistick_opts=opts.AxisTickOpts(is_show=False),
                                                                  axisline_opts=opts.AxisLineOpts(linestyle_opts=opts.LineStyleOpts(opacity=0))
                                                                  )
                                         )
        .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result5/result1.html")
    )
#6.画出美国确诊最少的10个州--词云图
def drawChart_6(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    data = []
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            row=(str(js['state']),int(js['sumCases']))
            data.append(row)
    d=(
        WordCloud().add("",data,word_size_range=[100,20],shape=SymbolType.DIAMOND).set_global_opts(title_opts=opts.TitleOpts(title="美国各州确诊最少的10个州"))
        .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result6/result1.html")
    )
#7.画出美国死亡最少的10个州--漏斗图
def drawChart_7(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    data = []
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            data.insert(0,[str(js['state']),int(js['sumDeaths'])])
    d=(
       Funnel().add("State",data,sort_="ascending",label_opts=opts.LabelOpts(position="inside")).set_global_opts(title_opts=opts.TitleOpts(title=""))
        .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result7/result1.html")
    )
#8.画出统计截止5.19全美和各州病死率--饼状图
def drawChart_8(index):
    root = "/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result" + str(index) + "/part-00000.json"
    values = []
    with open(root, 'r') as f:
        while True:
            line = f.readline()
            if not line:  # 到EOF，返回空字符串，终止循环
                break
            js = json.loads(line)
            if str(js['state'])=="USA":
                values.append(["Death(%)",round(float(js['deathRate'])*100,2)])
                values.append(["No-Death(%)", 100-round(float(js['deathRate']) * 100, 2)])
    d=(
       Pie().add("",values).set_global_opts(title_opts=opts.TitleOpts(title="全美病死率"))
       .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}:{c}"))
        .render("/home/qnstar/Documents/PycharmProjects/us_2019-covid/result/result8/result1.html")
    )

index=1
while index<9:
    funStr="drawChart_"+str(index)
    eval(funStr)(index)     #eval执行字符串表达式，python双括号理解：第一个函数返回了仍然是一个可以传入参数的函数
    index+=1
